const request = require('superagent')
const fs = require('fs')

const writeStream = fs.createWriteStream('tea.json')

const metainfo = {
  url: 'https://www.lagou.com/jobs/positionAjax.json',
  requestHeader: {
    'Accept': 'application/json',
    'Referer': 'https://www.lagou.com/jobs/list_%E5%89%8D%E7%AB%AF?labelWords=&fromSearch=true&suginput=',
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36',
    'Cache-Control': 'no-cache',
    'Origin': 'https://www.lagou.com',
    'Host': 'www.lagou.com',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Pragma': 'no-cache',
    'timeout': 5000
  },
  queryObj: {
    city: '成都',
    needAddtionalResult: false,
    isSchoolJob: 0
  },
  postObj: {
    first: true,
    pn: 1,
    kd: '前端'
  }
}

function requestInfo (queryObj, postObj, ms) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      request
        .post(metainfo.url)
        .set(metainfo.requestHeader)
        .query(queryObj)
        .send(postObj)
        .then(res => {
          const content = res.body.content
          if (content) {
            resolve(content)
          } else {
            reject('no content')
          }
        })
        .catch(err => {
          reject(err)
        })
    }, ms)
  })
}

async function getTotalInfo () {
  let { first, pn, kd } = metainfo.postObj
  let pageSize = 1
  let totalCount = 1000

  writeStream.write('[\n')

  while (pn * pageSize <= totalCount) {
    console.log(`第${pn}頁`)
    await requestInfo(metainfo.queryObj, { first, pn, kd }, 2000).then(content => {
      writeStream.write(JSON.stringify(content.positionResult.result).slice(1, -1))
      writeStream.write(',\n')
      pn++
      pageSize = content.pageSize
      first = false
      totalCount = content.positionResult.totalCount
    }).catch(err => {
      console.error(err)
    })
  }
  writeStream.write(']')
  writeStream.end()
}

getTotalInfo()
